import java.util.Arrays;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;

public class Test {
    private static final int K = 3;



//         求前K个最小的值(原始方法)
    public int[] smallestK(int[] arr,int k){
        int[] ret= new int[k];
        if(arr==null||k==0){
            return ret;
        }
        Queue<Integer> minHeap1 = new PriorityQueue<>(K);
        for (int x:arr) {
            minHeap1.offer(x);
        }
        for (int i=0;i<k;i++){
            ret[i]=minHeap1.poll();
        }
        return ret;
    }

//          求前k个最大的值(建小堆)
    public static int[] maxK(int []arr,int k){
        int[] ret = new int[k];
        if(arr==null || k==0){
            return  ret;
        }
//          求前K个最大值要建小根堆
        Queue<Integer> minHeap = new PriorityQueue<>(K);

        for(int i=0;i<k;i++){
            minHeap.offer(arr[i]);
        }
        for(int i=k;i<arr.length;i++){
            int val=minHeap.peek();
            if(val<arr[i]){
                minHeap.poll();
                minHeap.offer(arr[i]);
            }
        }
        for (int i=0;i<k;i++){
            ret[i]=minHeap.poll();
        }
        return ret;
    }
    public static void main3(String[] args) {
        int[] array={1,5,43,3,2,7,98,41,567,78};
        int[] ret=maxK(array,3);
        System.out.println(Arrays.toString(ret));
    }
    public static void main2(String[] args) {
         Queue<Integer> priorityQueue= new PriorityQueue<>();
        priorityQueue.offer(10);
        priorityQueue.offer(2);
        System.out.println(priorityQueue.poll());
    }
    public static void main1(String[] args) {

        TestHeap testHeap= new TestHeap();
        int[] array={27,15,19,18,28,34,65,49,25,37};
        testHeap.initElem(array);
        testHeap.createHeap();
//        testHeap.offer(80);
        testHeap.pop();
        System.out.println("dsada");
    }
}
